package cn.steam.foo.taks;

import cn.steam.bar.model.WeaponCreateModel;
import cn.steam.foo.feign.WeaponFeign;
import cn.steam.foo.model.TaskResult;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author zhang jintao on 2022/05/15
 **/
@Data
@Slf4j
public class Task implements Runnable {

    private WeaponCreateModel model;

    private TaskResult result;

    private StringRedisTemplate redisTemplate;

    WeaponFeign weaponFeign;

    @Override
    public void run() {
        try {
            Thread.sleep(50);
            weaponFeign.save(model);
            this.result.success();
        } catch (Exception e) {
            log.error("调用异常：", e);
            this.result.getFailedList().add(model.getName());
            this.result.failed();
        } finally {
            redisTemplate.opsForValue().set(result.getTaskId(), JSON.toJSONString(result));
            log.info("任务汇报:{}", result);
        }
    }

}